package com.lfy.modules.monitor;

import cn.hutool.extra.spring.SpringUtil;
import com.lfy.domain.anno.Describe;
import com.lfy.modules.monitor.service.SseService;
import com.lfy.modules.monitor.task.SseTaskCache;
import org.quartz.*;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Describe("cpu监控任务")
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class CpuMonitorTask implements Job {

    private static final SseService SSE_SERVICE;

    private static final ThreadPoolExecutor MAIN_THREAD_POOL;
    static {
        SSE_SERVICE = SpringUtil.getBean(SseService.class);

        MAIN_THREAD_POOL = SpringUtil.getBean("mainThreadPool");
    }
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        if(SseTaskCache.isEmpty()){
            return;
        }
        CompletableFuture.runAsync(SSE_SERVICE::sendSse,MAIN_THREAD_POOL);
    }
}
